<template>
  <template v-if="loaded">
    <ProForm
      ref="formRef"
      :model="formState"
      :items="items"
      :disabled="readonly"
      :onValuesChange="onValuesChange"
      :formColLayout="formColLayout()"
      :requiredMark="!readonly"
      class="pro-form-modal"
      v-bind="formProps"
    >
    </ProForm>
    <FooterButtons
      :formState="formState"
      :formRef="formRef"
      v-model:readonly="readonly"
      :serviceApi="_networkCheck"
      @refresh="refresh"
      :fastConfig="fastConfig"
    />
  </template>
</template>

<script setup lang="tsx">
import { computed, nextTick, ref, shallowRef } from 'vue';
import { Tag } from 'ant-design-vue';
import { ProForm, ProFormProps } from '@/components/ProForm';
import { ItemProps } from '@/components/ProForm/interface';
import { getNetworkCheck, networkCheck } from '@/api/device';
import FooterButtons from './FooterButtons.vue';

defineOptions({
  name: 'Ethernet',
});

interface Props {
  pageData: any;
  fastConfig?: boolean;
}
const props = defineProps<Props>();
const loaded = ref(false);
const readonly = ref(true);
const formRef = shallowRef();
const formState = ref<Record<string, any>>({});
const column = 3;
const formColLayout = () => {
  const num = 24 / column;
  return { xs: num, sm: num, md: num, lg: num };
};
const formProps = computed<ProFormProps>(() => {
  return {
    labelCol: { span: 6 },
    wrapperCol: { span: 20 },
  };
});
const onValuesChange = (
  values: Record<string, any>,
  state: Record<string, any>,
) => {
  formState.value = {
    ...formState.value,
    ...state,
  };
};
const refresh = () => {
  loaded.value = false;
  nextTick(() => {
    loaded.value = true;
  });
};
const itemObj = (
  label: string,
  type: any,
  field: string,
  required: boolean = false,
  props?: any,
  layout?: any,
  rules: any = [],
): ItemProps => {
  return {
    label,
    type,
    field,
    props: {
      placeholder: `请${type == 'input' ? '输入' : '选择'}${label}`,
      allowClear: false,
      ...props,
    },
    rules: [
      {
        required,
        message: `请${type == 'input' ? '输入' : '选择'}${label}`,
        trigger: 'blur',
      },
      ...rules,
    ],
    ...layout,
  };
};
const itemRender = (title: string): ItemProps => {
  return {
    label: { title },
    type: 'input',
    field: 'h5',
    formItemRender() {
      return <h5 class='h5'>{title}</h5>;
    },
  };
};

const items = computed<ProFormProps['items']>(() => [
  itemRender('网络调试'),
  itemObj('检查地址', 'input', 'checkUrl', true),
  itemObj('调试类型', 'select', 'checkType', true, {
    options: [
      { value: 0, label: 'PING' },
      { value: 1, label: 'TCP' },
      { value: 2, label: 'HTTP' },
    ],
  }),
  {
    label: '校验结果',
    type: 'input',
    field: 'checkResult',
    customRender(formState) {
      return formState.checkResult >= 0 ? (
        <Tag color={formState.checkResult == 1 ? 'success' : 'warning'}>
          {formState.checkResult == 1 ? '成功' : '失败'}
        </Tag>
      ) : (
        <>未知</>
      );
    },
  },
]);

getNetworkCheck(props.pageData.code).then((res: any) => {
  formState.value = res.data;
  formState.value.code = props.pageData.code;
  loaded.value = true;
});

const _networkCheck = async (params: any) => {
  const res: any = await networkCheck(params);
  formRef.value.setFiledValue('checkResult', res.data.checkResult);
};
</script>
